package com.aegean.CommercialHotelBookSystem.admin.businessImpl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;

import com.aegean.CommercialHotelBookSystem.admin.business.IAdminBusiness;
import com.aegean.CommercialHotelBookSystem.admin.dao.IAdminDao;
import com.aegean.CommercialHotelBookSystem.factory.DaoFactory;
import com.aegean.CommercialHotelBookSystem.model.Book;
import com.aegean.CommercialHotelBookSystem.model.BookDetail;
import com.aegean.CommercialHotelBookSystem.model.Customer;
import com.aegean.CommercialHotelBookSystem.model.Service;
import com.aegean.CommercialHotelBookSystem.model.component.Photo;
import com.aegean.CommercialHotelBookSystem.user.business.IUserBusiness;
import com.aegean.CommercialHotelBookSystem.user.dao.IUserDao;
import com.aegean.CommercialHotelBookSystem.user.dao.Impl.UserDaoImpl;

public class AdminBusinessImpl implements IAdminBusiness {

	@Override
	public boolean addAdmin(String dir, String picName, String fileName,
			String fileType, String User_Name, String User_RealName,
			String User_Pwd, String User_Sex, String service_No) {
		Service one = new Service();
		Photo photo = new Photo();
		try {
			photo.setPhoto_Name(fileName);
			photo.setPhoto_Type(fileType);
			File Service_Photo = new File(dir + "/upload/Service/" + picName);
			InputStream in = new FileInputStream(Service_Photo);
			photo.setPhoto(Hibernate.createBlob(in));
			one.setPerson_Login(User_Name);
			one.setPerson_Name(User_RealName);
			one.setPerson_Password(User_Pwd);
			one.setPerson_Sex(User_Sex.charAt(0));
			one.setPerson_Photo(photo);
			one.setService_No(Integer.parseInt(service_No));
			IAdminDao servicedao = DaoFactory.createAdminDao();
			servicedao.saveObject(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean editAdmin(Service one, String dir, String fileName,
			String fileType, String User_Name, String User_RealName,
			String User_Pwd, String User_Sex, String service_No) {
		try {
			if (fileName != null) {
				Photo photo = new Photo();
				photo.setPhoto_Name(fileName);
				photo.setPhoto_Type(fileType);
				File Service_Photo = new File(dir + "/upload/Service/" + fileName);
				InputStream in = new FileInputStream(Service_Photo);
				photo.setPhoto(Hibernate.createBlob(in));
				one.setPerson_Photo(photo);
			}
			one.setPerson_Login(User_Name);
			one.setPerson_Name(User_RealName);
			one.setPerson_Sex(User_Sex.charAt(0));
			one.setService_No(Integer.parseInt(service_No));
			IAdminDao admindao = DaoFactory.createAdminDao();
			admindao.updateObject(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean editPwd(Service one, String newPwd) {
		try {
			one.setPerson_Password(newPwd);
			IAdminDao admindao = DaoFactory.createAdminDao();
			admindao.updatePwd(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Service loginAdmin(String User_Login, String User_Pwd) {
		IAdminDao admindao = DaoFactory.createAdminDao();
		Service one = admindao.searchService(User_Login, User_Pwd);
		return one;
	}

	@Override
	public boolean searchAdmin(String No) {
		IAdminDao dao=DaoFactory.createAdminDao();
		return dao.searchAdmin(No);
	}
}
